package cn.luow.mail.service.imel;

import cn.luow.mail.config.MailConfig;
import cn.luow.mail.service.IMailService;
import com.alibaba.fastjson.JSON;
import freemarker.template.Template;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.ssssssss.magicapi.core.annotation.MagicModule;
import org.ssssssss.script.annotation.Comment;

import javax.annotation.Resource;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
@Service("mailService")
@MagicModule("mail")    // 模块名称
public class MailServiceImpl implements IMailService {
    @Resource
    private MailConfig mailConfig;

    //注入MailSender
    @Resource
    private JavaMailSender mailSender;

    //发送邮件的模板引擎
    @Resource
    private FreeMarkerConfigurer configurer;




    @Override
    @Comment("发送简单邮件")
    public void sendSimpleMail(@Comment(name="title",value = "标题")String title,
                               @Comment(name="content",value = "内容")String content) {
        this.sendSimpleMail(title,content,mailConfig.getTo());
    }

    @Override
    @Comment("发送简单邮件")
    public void sendSimpleMail(@Comment(name="title",value = "标题")String title,
                               @Comment(name="content",value = "内容")String content,
                               @Comment(name="to",value = "发送人")String to) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setFrom("admin@luow.fun");
            message.setSubject(mailConfig.getWebName()+title);
            message.setText(content);
            mailSender.send(message);
            log.info("【{}】邮件发送成功,接收人{}",mailConfig.getWebName()+title,to);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Comment("默认模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="content",value = "内容")String content) {
        Map<String ,Object> params = new HashMap<>();
        params.put("title",title);
        params.put("content",content);
        this.send(title,mailConfig.getDefaultFtl(),params,mailConfig.getTo());
    }
    @Override
    @Comment("默认模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="content",value = "内容")String content,
                     @Comment(name="to",value = "发送人")String to) {
        Map<String ,Object> params = new HashMap<>();
        params.put("title",title);
        params.put("content",content);
        this.send(title,mailConfig.getDefaultFtl(),params,to);
    }
    @Override
    @Comment("默认模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="params",value = "参数")Object params) {
        if(params instanceof String){
            this.send(title,(String) params);
        }
        this.send(title,mailConfig.getDefaultFtl(),params,mailConfig.getTo());

    }

    @Override
    @Comment("使用模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="templateName",value = "模板")String templateName,
                     @Comment(name="params",value = "参数")Object params) {
        this.send(title,templateName,params,mailConfig.getTo());
    }

    @Override
    @Comment("使用模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="templateName",value = "模板")String templateName,
                     @Comment(name="params",value = "参数")Object params,
                     @Comment(name="to",value = "发送人")String to) {
        Map<String, Object> model = (Map<String, Object>) JSON.parse(JSON.toJSONString(params));
        this.send(title,templateName,model,to);
    }

    @Override
    @Comment("默认模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="model",value = "参数")Map<String, Object> model) {
        this.send(title,mailConfig.getDefaultFtl(),model,mailConfig.getTo());
    }

    @Override
    @Comment("使用模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="templateName",value = "模板")String templateName,
                     @Comment(name="model",value = "参数")Map<String, Object> model) {
        this.send(title,templateName,model,mailConfig.getTo());
    }

    @Override
    @Comment("使用模板发送邮件")
    public void send(@Comment(name="title",value = "标题")String title,
                     @Comment(name="templateName",value = "模板")String templateName,
                     @Comment(name="model",value = "参数")Map<String, Object> model,
                     @Comment(name="to",value = "发送人")String to) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(mailConfig.getFrom());
            helper.setTo(InternetAddress.parse(to));//发送给谁
            helper.setSubject(mailConfig.getWebName()+title);//邮件标题
            Template template = configurer.getConfiguration().getTemplate(templateName+".ftl");
            model.put("mailConfig",mailConfig);
            //赋值后的模板邮件内容
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(text, true);//邮件内容
            mailSender.send(mimeMessage);
            log.info("【{}】邮件发送成功,接收人{}",mailConfig.getWebName()+title,to);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
